<?php
//退货单
class TuihuoController extends Controller
{
    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_tuihuo where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select c.*,u.realname from crm_tuihuo c left join crm_user
 u on c.customer_id = u.user_id where c.deleted = 0  limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $tuihuo_id = $_REQUEST['tuihuo_id'];

        $potentials = null;
        if($tuihuo_id==0){
            //新增
        }else{
            //编辑
            $potentials = $this->connection->createCommand("select c.*,u.realname from crm_tuihuo c left join 
crm_user u on c.customer_id = u.user_id  where c.tuihuo_id = :tuihuo_id")->bindParam(':tuihuo_id',$tuihuo_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['potentials'=>$potentials]);
    }

    public function actionSavedata(){

        $tuihuo_id = $_REQUEST['tuihuo_id'];
        $tuihuo_num = $_REQUEST['tuihuo_num'];
        $customer_id = $_REQUEST['customer_id'];
        $tuihuo_date = $_REQUEST['tuihuo_date'];
        $total = $_REQUEST['total'];
        $approve_status = $_REQUEST['approve_status'];

        if($tuihuo_id!=null){
            //编辑
            $this->connection->createCommand("update crm_tuihuo set tuihuo_num = :tuihuo_num,customer_id = :customer_id
,tuihuo_date = :tuihuo_date,total = :total,approve_status = :approve_status 
where tuihuo_id = :tuihuo_id")
                ->bindParam(':tuihuo_id',$tuihuo_id,PDO::PARAM_INT)
                ->bindParam(':tuihuo_num',$tuihuo_num,PDO::PARAM_STR)
                ->bindParam(':customer_id',$customer_id,PDO::PARAM_INT)
                ->bindParam(':tuihuo_date',$tuihuo_date,PDO::PARAM_STR)
                ->bindParam(':total',$total,PDO::PARAM_STR)
                ->bindParam(':approve_status',$approve_status,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_tuihuo (tuihuo_num,customer_id,tuihuo_date,total,approve_status) values 
(:tuihuo_num,:customer_id,:tuihuo_date,:total,:approve_status,:nextstep) ")
                ->bindParam(':tuihuo_num',$tuihuo_num,PDO::PARAM_STR)
                ->bindParam(':customer_id',$customer_id,PDO::PARAM_INT)
                ->bindParam(':tuihuo_date',$tuihuo_date,PDO::PARAM_STR)
                ->bindParam(':total',$total,PDO::PARAM_STR)
                ->bindParam(':approve_status',$approve_status,PDO::PARAM_STR)
                ->execute();
        }
    }


    public function actionRemove(){
        $tuihuo_id = $_REQUEST['tuihuo_id'];
        $this->connection->createCommand("update crm_tuihuo set deleted = 1 
where tuihuo_id = :tuihuo_id")
            ->bindParam(':tuihuo_id',$tuihuo_id,PDO::PARAM_INT)
            ->execute();
    }
}